<template>
	<view>
		<view class="login">
			<image class="bg" src="https://master.shengejiaju.cn/web/static/icons/login-bg-1.png"></image>
			<!-- <view class="login-top">
				<view class="login-top-ico"><image src="https://master.shengejiaju.cn/web/static/icons/logo.png"></image></view>
				<view class="login-top-tit">家具大师商户端</view>
			</view> -->
			<view class="login-bottom">
				<!-- 微信小程序登录 -->
				<button class="phone-login" v-if="isMiniProgram || isWxH5" open-type="getPhoneNumber" @getphonenumber="wechatLogin">快速登录</button>
				<!-- 微信小程序登录 -->
				<!-- Apple 登录 -->
				<button class="isAppleLogin" @click="signInApple" v-if="isAppleLogin"><image src="https://master.shengejiaju.cn/web/static/icons/apple.png"></image>Sign in with Apple</button>
				<!-- Apple 登录 -->
				<button :class="isMiniProgram || isH5?'phone-login mini-program-phone':'phone-login'" @click="phoneLogin">手机号登陆</button>
				<!-- <view class="enter-phone-agree" @click="agreeFun">
					<image src="https://master.shengejiaju.cn/web/static/icons/ico_seleced01@2x.png" v-if="!agree" mode="aspectFill"></image>
					<image src="https://master.shengejiaju.cn/web/static/icons/ico_selcted@2x.png"  mode="aspectFill" v-else></image>
					<view class="enter-phone-agree-text">
						我已同意
						<text @click.stop="showProtocol">用户协议和隐私政策</text>
						，未注册手机验证后即注册
					</view>
				</view> -->
				<view class="others-login" v-if="isAppPlus">
					<view class="others-login-tit">第三方登录</view>
					<view class="wechat-login" @click="wechatLogin">
						<view><image src="https://master.shengejiaju.cn/web/static/icons/ico_wechat.png"></image></view>
						<view><text>微信登录</text></view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				agree: true,
				isAppleLogin: false,
				isAppPlus: false,
				isMiniProgram: false,
				isH5: false,
				isWxH5: false,
				weixinCode: '',
				code: ''
			}
		},
		onLoad(){
			// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					this.code = loginRes.code
				}
			})
			// #endif
			if(uni.getStorageSync('userInfo')){
				let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
				if(userInfo.token){
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			}
		},
		methods: {
			agreeFun(){
				this.agree = !this.agree
			},
			showProtocol(){
				uni.navigateTo({
					url: '/pages/webview/webview?url='+encodeURIComponent('https://master.jiaju01.com/web/merchant/#/pages/news/news-info?id=28')
				})
				// this.$refs.protocol.open()
			},
			showPrivacy(){
				uni.navigateTo({
					url: '/pages/webview/webview?url='+encodeURIComponent('https://master.jiaju01.com/web/merchant/#/pages/news/news-info?id=28')
				})
				// this.$refs.privacy.open()
			},
			phoneLogin(){
				if(!this.agree){
					uni.showToast({
						icon: 'none',
						title: '请阅读并勾选用户协议及隐私政策'
					})
					return false
				}
				uni.navigateTo({
					url: '/pages/login/login-1'
				})
			},
			wechatLogin(e){
				let _this = this
				if(!this.agree){
					uni.showToast({
						icon: 'none',
						title: '请阅读并勾选用户协议及隐私政策'
					})
					return false
				}
				// #ifdef H5
				uni.showToast({
					icon: 'none',
					title: 'H5微信授权登录暂未上线，请使用手机号登录或在小程序中使用微信登录',
					duration: 2000
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.checkSession({
					success: () => {
						uni.login({
						  provider: 'weixin',
						  success: function (loginRes) {
								_this.code = loginRes.code
								let data = {
									code: _this.code,
									parent_id: uni.getStorageSync('parent_id')?uni.getStorageSync('parent_id'):0,
									encryptedData: e.detail.encryptedData,
									iv:  e.detail.iv,
									client_type: 2
								}
								_this.$api.WeChatBinding(data).then(res => {
									if(res.datas.role_type!=2 && res.datas.role_type!=11){
										return uni.showModal({
											title: '温馨提示',
											content: '您还不是商家管理员 请联系总部',
											showCancel: false
										})
									}
									uni.setStorageSync('userInfo',JSON.stringify(res.datas))
									uni.showToast({
										title: '登录成功',
										icon: 'none',
										duration: 1500
									})
									setTimeout(() => {
										if(uni.getStorageSync('loginUrl')){
											uni.switchTab({
												url: uni.getStorageSync('loginUrl')
											})
										}else{
											uni.switchTab({
												url: '/pages/index/index'
											})
										}
									},1500)
								})
						  },
							fail: function(fail) {
								uni.showToast({
									title: '登录失败，请联系平台相关人员!',
									icon: 'none',
									duration: 1500
								})
							}
						});
					},
					fail: () => {
						uni.login({
							provider: 'weixin',
							success: (loginRes) => {
								this.code = loginRes.code
							}
						})
					}
				})
				// #endif
			},
			signInApple(){
				uni.login({
					provider: 'apple',
					success: (loginRes) => {
						uni.getUserInfo({
							provider: 'apple',
							success: (appleUserInfo) => {
								let data = {
									user: plus.device.uuid
								}
								this.$api.WxIosLogin(data).then(res => {
									let iosLoginRes = {}
									iosLoginRes.token = res.datas.token
									iosLoginRes.role_type = 2
									iosLoginRes.user_id = res.datas.user_id
									iosLoginRes.store_id = res.datas.store_id
									iosLoginRes.heard_img = res.datas.heard_img,
									iosLoginRes.nickname = res.datas.nickname,
									iosLoginRes.store_logo = res.datas.store_logo,
									iosLoginRes.store_name = res.datas.store_name,
									iosLoginRes.ios = true
									uni.setStorageSync('userInfo',JSON.stringify(iosLoginRes))
									uni.showToast({
										icon: 'none',
										title: '登录成功',
										duration: 1000
									})
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/index/index'
										})
									},1000)
								})
							}
						})
					},
					fail: (err) => {
						uni.showToast({
							icon: 'none',
							title: '登录失败'
						})
					}  
				})
			}
		},
		created() {
			
			// #ifdef APP-PLUS
				// 请求接口是否为审核状态，审核状态开启并且IOS系统在ios13及以上开启第三方登录(微信、apple)
				let systemInfo = uni.getSystemInfoSync()
				let isAudit = false
				this.$api.HomeWeChat().then(res => {
					if(res.datas.is_show_store_app==2){
						isAudit = true
					}
					if(systemInfo.system.indexOf('13') != -1 && isAudit){
						this.isAppleLogin = true
					}else{
						this.isAppleLogin = false
					}
				})
				// IOS判断第三方程序(微信) 是否安装
				if(systemInfo.platform==='ios'){
					if(plus.runtime.isApplicationExist({pname:'com.tencent.mm',action:'weixin://'})){
						this.isAppPlus = true
					}else{
						this.isAppPlus = false
					}
				}
				// 安卓平台始终开启微信登录按钮
				if(systemInfo.platform==='android'){
					this.isAppPlus = true
				}
			// #endif
			
			// #ifdef H5
			this.isH5 = true
			let ua = navigator.userAgent.toLowerCase()
			if (ua.match(/MicroMessenger/i) == "micromessenger") {
				this.isWxH5 = true
			}
			// #endif
			
			// #ifdef MP-WEIXIN
			this.isMiniProgram = true
			// #endif
		}
	}
</script>

<style lang="scss">
	@import '../../static/scss/login.scss';
</style>